import Vue from 'vue';
import VueRouter from 'vue-router';
import HomeView from '../views/HomeView.vue';
import { isLogined } from '../tools/util';

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
  },
  {
    path: '/list/:id?',
    name: 'VueYimutianList',
    component: () => import('../views/List.vue'),
  },
  {
    path: '/shopCar',
    name: 'VueYimutianShopCar',
    component: () => import('../views/ShopCar.vue'),
    meta: {
      needLogin: true,
    },
  },
  {
    path: '/user',
    name: 'VueYimutianUser',
    component: () => import('../views/User.vue'),
    meta: {
      needLogin: true,
    },
  },
  {
    path: '/login',
    name: 'VueYimutianLogin',
    component: () => import('../views/Login.vue'),
    meta: {
      TarisShowed: true,
    },
  },
  {
    path: '/register',
    name: 'VueYimutianResg',
    component: () => import('../views/Resg.vue'),
    meta: {
      TarisShowed: true,
    },
  },
  {
    path: '/detail/:id',
    name: 'VueYimutianDetail',
    component: () => import('../views/Detail.vue'),
    meta: {
      TarisShowed: true,
    },
  },
  {
    path: '/orders',
    name: 'WorkspaceJsonOrders',
    component: () => import('../views/OrdersList.vue'),
    meta: {
      TarisShowed: true,
    },
  },
  {
    path: '/ordersDetails',
    name: 'WorkspaceJsonOrderDetails',
    component: () => import('../views/OrderDetails.vue'),
    meta: {
      TarisShowed: true,
    },
  },
  {
    path: '/alertUser',
    name: 'WorkspaceJsonAlertUser',
    component: () => import('../views/AlertUser.vue'),
    meta: {
      TarisShowed: true,
    },
  },
  {
    path: '/alertPassword',
    name: 'WorkspaceJsonAlertPassword',
    component: () => import('../views/AlertPassword.vue'),
    meta: {
      TarisShowed: true,
    },
  },
];
// const isLogined = false;
const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.meta.needLogin) {
    if (isLogined()) {
      next();
    } else {
      next({ name: 'VueYimutianLogin' });
    }
  } else {
    next();
  }
});

export default router;
